public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val=val;
        }
    }
    public TreeNode root;

    /**
     * 时间复杂度：O(logN)(最好情况)
     * O(N)(最坏情况)
     * @param val
     * @return
     */
    public boolean search(int val){
        TreeNode cur=root;
        while(cur!=null){
            if(cur.val<val){
                cur=cur.right;
            }else if(cur.val>val){
                cur=cur.left;
            }else{
                return true;
            }
        }
        return false;
    }
    public void insert(int val){
        TreeNode node=new TreeNode(val);
        if(root==null){
            root=node;
            return;
        }
        TreeNode cur=root;
        TreeNode parent=null;
        while(cur!=null){
            if(cur.val>val){
                parent=cur;
                cur=cur.left;
            }else if(cur.val<val){
                parent=cur;
                cur=cur.right;
            }else{
                return;
            }
        }
        if(parent.val>val){
            parent.left=node;
        }else{
            parent.right=node;
        }
    }
    
}
